<?php


// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\common\model;

use app\common\model\TimeModel;


class MallMember extends TimeModel
{


    /**
     * 根据openid查询记录
     * @param string $openid
     * @return Wechat|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWechatByOpenid(string $openid = '')
    {
        return $this->where('openid', $openid)
            ->find();
    }

    /**
     * @param string $openid
     * @return Wechat|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOpenidById(string $openid = '')
    {
        return $this->where('openid', $openid)
            ->find();
    }

    /**
     * @param array $where
     * @return Wechat|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIndexWechatByCount(array $where = [])
    {
        return $this->field('id,count(*) as count')
            ->where($where)
            ->find();
    }

    /**
     * @param string $nickName
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWechatByNickName(string $nickName = '', string $field = '*'): array
    {
        return $this->field($field)
            ->where('nickname', 'like', "%{$nickName}%")
            ->select()
            ->toArray();
    }
}